﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.Windows.Threading;

namespace AdvStopWatch
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    ///  My Blog : http://www.itblogs.info
    /// </summary>
    public partial class MainWindow : Window
    {
        private DispatcherTimer sysTimer;
        public bool isStart = false;
        public long totalSecond = 0;
        private List<Record> Records;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            sysTimer = new DispatcherTimer();
            sysTimer.IsEnabled = true;
            sysTimer.Interval = new TimeSpan(0, 0, 1);
            sysTimer.Tick += new EventHandler(sysTimer_Tick);
            Records = new List<Record>();

            lstTime.ItemsSource =null;
            lstTime.ItemsSource = Records;
           
        }

        void sysTimer_Tick(object sender, EventArgs e)
        {
            if (isStart)
            {
                totalSecond += 1;
                int hour = (int)Math.Round((decimal)totalSecond / 3600);
                int minute = (int)Math.Round((decimal)totalSecond % 3600) / 60;
                int second = (int)(totalSecond - (hour * 3600) - (minute * 60));
                lblHour.Content = hour;
                lblMinute.Content = minute;
                lblSecond.Content = second;
            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            isStart = true;
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            if (isStart)
            {
                isStart = false;

                Record rec = new Record();
                rec.Hour = (int)Math.Round((decimal)totalSecond / 3600);
                rec.Minute = (int)Math.Round((decimal)totalSecond % 3600) / 60;
                rec.Second = (int)(totalSecond - (rec.Hour * 3600) - (rec.Minute * 60));
                Records.Add(rec);
                lstTime.ItemsSource = null;
                lstTime.ItemsSource = Records;
            }
           
           
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            if (isStart)
            {

                Record rec = new Record();
                rec.Hour = (int)Math.Round((decimal)totalSecond / 3600);
                rec.Minute = (int)Math.Round((decimal)totalSecond % 3600) / 60;
                rec.Second = (int)(totalSecond - (rec.Hour * 3600) - (rec.Minute * 60));
                Records.Add(rec);
            }
            lstTime.ItemsSource = null;
            lstTime.ItemsSource = Records;
            totalSecond =0;
            lblHour.Content = "0";
            lblMinute.Content = "0";
            lblSecond.Content = "0";
        }

      
    }
}
